package com.github.tifezh.kchart;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.github.tifezh.kchart.chart.MinuteLineEntity;
import com.github.tifezh.kchart.mock.BaseInfo;
import com.github.tifezh.kchart.mock.TimeLineItemListBean;
import com.github.tifezh.kchart.mock.TradePeriodsBean;
import com.github.tifezh.kchart.mock.ValueBean;
import com.github.tifezh.kchartlib.chart.MinuteChartView;
import com.github.tifezh.kchartlib.chart.MinuteView;
import com.github.tifezh.kchartlib.chart.model.TimeInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tifezh on 2017/7/20.
 */

public class MinuteActivity extends AppCompatActivity {


    @BindView(R.id.minuteChartView)
    MinuteView mChartView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_minute_chart2);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
    }

    private void initData() {

        BaseInfo baseInfo = DataRequest.getMinuteData(this);

        ValueBean bean = baseInfo.value.get(0);
        List<MinuteLineEntity> data = new ArrayList<>();

        List<TimeInfo> timeInfos = new ArrayList<>();
        int count = 0;
        for (TradePeriodsBean item : bean.tradePeriods) {
            timeInfos.add(new TimeInfo(item.startTime, item.endTime));
            count += item.endTime - item.startTime;
        }
        System.out.println(count / 1000 / 60);

        long startTime = 0;
        long endTime = 0;
        int timeIndex = -1;
        int timeS = 1000 * 60;

        for (TimeLineItemListBean info : bean.timeLineItemList) {
            if (info.time > endTime) {
                System.out.println("休息时间");
                timeIndex++;
                startTime = bean.tradePeriods.get(timeIndex).startTime;
                endTime = bean.tradePeriods.get(timeIndex).endTime;
            }
            if (info.time < startTime) {
                System.out.println("跳过");
                continue;
            }

            while (info.time > startTime) {
                System.out.println("补数据" + startTime / 1000 / 60);
                //数据复制
                startTime += timeS;
                data.add(getEntity(info, startTime));
            }
            data.add(toEntity(info));
            System.out.println(startTime / 1000 / 60);
            startTime += timeS;
        }

        mChartView.initData(data,
                timeInfos, baseInfo.value.get(0).lastclose.floatValue());
    }

    public static MinuteLineEntity getEntity(TimeLineItemListBean bean, long startTime) {
        MinuteLineEntity entity = new MinuteLineEntity();
        entity.price = bean.current.floatValue();
        entity.time = new Date(startTime);
        return entity;
    }

    public static MinuteLineEntity toEntity(TimeLineItemListBean bean) {
        MinuteLineEntity entity = new MinuteLineEntity();
        entity.price = bean.current.floatValue();
        entity.time = new Date(bean.time);
        return entity;
    }
}
